package com.zone.vehicle.superApps.datasource;

import android.content.Context;

import com.zone.lib.download.DownloadHelper;
import com.zone.lib.download.DownloadStatus;
import com.zone.lib.download.db.DownloadDaoUtilsStore;
import com.zone.lib.download.db.entity.Download;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.vehicle.superApps.db.DaoUtilsStore;
import com.zone.vehicle.superApps.db.entity.App;
import com.zone.vehicle.superApps.db.gen.AppDao;
import com.zone.vehicle.superApps.enums.AppTypeEnum;
import com.zone.vehicle.superApps.utils.SharedPreferencesUtil;

import org.apache.commons.lang3.ObjectUtils;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * Class that handles authentication w/ login credentials and retrieves user information.
 */
public class AppManagerDataSource {

    /**
     * 获取执行中APP：下载、安装
     * @param type
     * @return
     */
    public List<App> getActivityApp(AppTypeEnum type){
        List<App> list = DaoUtilsStore.getInstance().getAppDaoUtils().queryByQueryBuilder(AppDao.Properties.Type.eq(type.getCode()));
        return list;
//        return null;
    }

    public List<App> getInstalledApp(AppTypeEnum type){
        // TODO 未接入安装，暂时将下载完成状态APP查询出来
        List<App> list = DaoUtilsStore.getInstance().getAppDaoUtils().queryByQueryBuilder(AppDao.Properties.Type.eq(type.getCode()), AppDao.Properties.State.eq(DownloadStatus.COMPLETE_DOWNLOAD));
        return list;
    }

    public void downLoadApp(Context context, int status, AppEntity appEntity){
        if (ObjectUtils.isNotEmpty(appEntity)){
//            File dir = new File(context.getExternalCacheDir(), "download");
            File dir = new File(context.getCacheDir(), "download");
            if (!dir.exists()){
                dir.mkdirs();
            }
            File saveFile = new File(dir, appEntity.getCode()+".apk");
            Download download = DownloadDaoUtilsStore.getInstance().getDownloadByCode(appEntity.getCode());
            if (ObjectUtils.isNotEmpty(download)){
                DownloadDaoUtilsStore.getInstance().deleteByCode(download.getCode());
                download = null;
            }
            if (ObjectUtils.isEmpty(download)){
                download = new Download();
                download.setCode(appEntity.getCode());
                download.setDownloadStatus(status);
                download.setDownloadUrl(appEntity.getUrl());
                download.setFilePath(saveFile.getPath());
                download.setAppType("0");
                download.setType("0");
                download.setDownloadLocation(0);
                download.setType(""+appEntity.getType());
                download.setVersion(appEntity.getVersion());
                download.setCreateTime(new Date().toString());
                DownloadDaoUtilsStore.getInstance().getDownloadAppDaoUtils().insert(download);
//                DownloadHelper.getInstance().addTask(appEntity.getCode(), saveFile, status, Long.parseLong(ObjectUtils.isEmpty(appEntity.getSize())?"0":appEntity.getSize()), appEntity.getVersion(), SharedPreferencesUtil.getInstance(context).getSP("vin")).submit(context);
                DownloadHelper.getInstance().addTask(saveFile, SharedPreferencesUtil.getInstance(context).getSP("vin"), status, appEntity, download).submit(context);
            } else {
                //暂停 或者 继续
                if (status == DownloadStatus.PAUSE_DOWNLOAD || status == DownloadStatus.PREPARE_DOWNLOAD
//                        (download.getDownloadStatus() == DownloadStatus.PREPARE_DOWNLOAD
//                    || download.getDownloadStatus() == DownloadStatus.LOADING_DOWNLOAD)
                ){
//                    DownloadHelper.getInstance().addTask(appEntity.getCode(), saveFile, status, Long.parseLong(appEntity.getSize()), appEntity.getVersion(), SharedPreferencesUtil.getInstance(context).getSP("vin")).submit(context);
                    DownloadHelper.getInstance().addTask(saveFile, SharedPreferencesUtil.getInstance(context).getSP("vin"), status, appEntity, download).submit(context);
                }
            }
        }
    }
}